package com.ruoyi.match.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.match.domain.dto.InsertUserDto;
import com.ruoyi.match.domain.dto.MatchUserDto;
import com.ruoyi.match.domain.vo.MatchUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.match.mapper.MatchUserMapper;
import com.ruoyi.match.domain.MatchUser;
import com.ruoyi.match.service.IMatchUserService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.DigestUtils;

/**
 * 用户Service业务层处理
 * 
 * @author 星野漫游
 * @date 2024-06-02
 */
@Service
public class MatchUserServiceImpl implements IMatchUserService 
{
    @Autowired
    private MatchUserMapper matchUserMapper;

    /**
     * 查询用户
     * 
     * @param userId 用户主键
     * @return 用户
     */
    @Override
    public MatchUser selectMatchUserByUserId(Long userId)
    {
        return matchUserMapper.selectMatchUserByUserId(userId);
    }

    /**
     * 查询用户列表
     * 
     * @param matchUser 用户
     * @return 用户
     */
    @Override
    public List<MatchUserVo> selectMatchUserList(MatchUserDto matchUser)
    {
        MatchUser userF = new MatchUser();
        BeanUtils.copyProperties(matchUser,userF);
        List<MatchUser> list=matchUserMapper.selectMatchUserList(userF);

        ArrayList<MatchUserVo> userVos = new ArrayList<>();

        list.forEach(user -> {
            MatchUserVo matchUserVo = new MatchUserVo();
            BeanUtils.copyProperties(user,matchUserVo);
            userVos.add(matchUserVo);
        });


        return userVos;
    }

    /**
     * 新增用户
     *
     * @param insertUserDto 用户
     * @return 结果
     */
    @Override
    public int insertMatchUser(InsertUserDto insertUserDto)
    {
        insertUserDto.setCreateTime(DateUtils.getNowDate());
        //对用户密码加盐加密
        String password = insertUserDto.getPassword();
        String salt =getRandomString2(2);
        String pswd = DigestUtils.md5DigestAsHex((password + salt).getBytes());
        insertUserDto.setPassword(pswd);

        MatchUser matchUser = new MatchUser();
        BeanUtils.copyProperties(insertUserDto,matchUser);
        matchUser.setSalt(salt);

        return matchUserMapper.insertMatchUser(matchUser);
    }

    /**
     * 修改用户
     *
     * @param insertUserDto 用户
     * @return 结果
     */
    @Override
    public int updateMatchUser(InsertUserDto insertUserDto)
    {
        insertUserDto.setUpdateTime(DateUtils.getNowDate());

        MatchUser matchUser = new MatchUser();
        //对密码加盐加密
        MatchUser matchSalt=matchUserMapper.getByUserName(insertUserDto.getUserName());
        String salt = matchSalt.getSalt();
        String pswd = DigestUtils.md5DigestAsHex((insertUserDto.getPassword() + salt).getBytes());

        BeanUtils.copyProperties(insertUserDto,matchUser);
        matchUser.setPassword(pswd);
        return matchUserMapper.updateMatchUser(matchUser);
    }

    /**
     * 批量删除用户
     * 
     * @param userIds 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteMatchUserByUserIds(String userIds)
    {
        return matchUserMapper.deleteMatchUserByUserIds(Convert.toStrArray(userIds));
    }

    /**
     * 删除用户信息
     * 
     * @param userId 用户主键
     * @return 结果
     */
    @Override
    public int deleteMatchUserByUserId(Long userId)
    {
        return matchUserMapper.deleteMatchUserByUserId(userId);
    }



    //可以指定字符串的某个位置是什么范围的值
    private String getRandomString2(int length) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(3);
            long result = 0;
            switch (number) {
                case 0:
                    result = Math.round(Math.random() * 25 + 65);
                    sb.append(String.valueOf((char) result));
                    break;
                case 1:
                    result = Math.round(Math.random() * 25 + 97);
                    sb.append(String.valueOf((char) result));
                    break;
                case 2:
                    sb.append(String.valueOf(new Random().nextInt(10)));
                    break;
            }


        }
        return sb.toString();

    }
}
